Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

173
Views
Hoc React Hook "useEffect" cannot be called inside a callback

I am trying to use an HOC component in React with a functional component, but when using hooks I keep getting the error React Hook "useEffect" cannot be called inside a callback. What am I doing wrong and how can I solve this?

const HOC = (WrappedComponenet, entity) => {
  return function () {
    const [data, setData] = useState([]);
    const [term, setTerm] = useState("");

    useEffect(() => {
      fetch(`https://jsonplaceholder.typicode.com/${entity}`)
        .then((res) => res.json())
        .then((data) => setData(data));
    }, []);

    let filteredData = data.slice(0, 10).filter((d) => {
      if (entity === "users") {
        const { name } = d;
        return name.indexOf(term) >= 0;
      }
      if (entity === "todos") {
        const { title } = d;
        return title.indexOf(term) >= 0;
      }
    });

    return <WrappedComponenet data={filteredData} />;
  };
};
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Check out this snippet:

const HOC = (WrappedComponenet, entity) => {
  function MyCustomComponent() {
    const [data, setData] = useState([]);
    const [term, setTerm] = useState("");

    useEffect(() => {
      fetch(`https://jsonplaceholder.typicode.com/${entity}`)
        .then((res) => res.json())
        .then((data) => setData(data));
    }, []);

    let filteredData = data.slice(0, 10).filter((d) => {
      if (entity === "users") {
        const { name } = d;
        return name.indexOf(term) >= 0;
      }
      if (entity === "todos") {
        const { title } = d;
        return title.indexOf(term) >= 0;
      }
    });

    return <WrappedComponenet data={filteredData} />;
  };

  return <MyCustomComponent/>
};
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!